package a51qindian.com.comprehensive.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.li.interfaces.OnItemClickListener;
import com.li.widget.MultipleLayout;
import com.li.widget.TopNavigationBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import a51qindian.com.comprehensive.Const;
import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.adapter.ChooseShopAdapter;
import a51qindian.com.comprehensive.base.QDBaseActivity;
import a51qindian.com.comprehensive.items.LinearItemDecoration;
import a51qindian.com.comprehensive.model.CountBaseModel;
import a51qindian.com.comprehensive.model.ShopInfo;
import a51qindian.com.comprehensive.net.ObservableHelper;
import a51qindian.com.comprehensive.net.RetrofitCreator;
import a51qindian.com.comprehensive.net.SelectorObserver;
import butterknife.BindView;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;


/**
 * Author: Li
 * Date: 2017/9/25 0025
 * Description: 首装分配时的选择店铺界面
 */

public class ChooseShopActivity extends QDBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.recycler_shop)
    RecyclerView mRecyclerShop;
    @BindView(R.id.layout_content)
    MultipleLayout mLayoutContent;
    @BindView(R.id.layout_refresh)
    SmartRefreshLayout mLayoutRefresh;

    private List<ShopInfo> mShopInfoList;
    private ChooseShopAdapter mChooseShopAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_choose_shop;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);
        mShopInfoList = new ArrayList<>();
        mRecyclerShop.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerShop.addItemDecoration(new LinearItemDecoration());
        mChooseShopAdapter = new ChooseShopAdapter(this);
        mRecyclerShop.setAdapter(mChooseShopAdapter);
        mChooseShopAdapter.setItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int pos) {
                ShopInfo info = mShopInfoList.get(pos);
                Intent data = new Intent();
                data.putExtra("shopId", info.getShop_id());
                data.putExtra("shopName", info.getName());
                data.putExtra("shopAddress",info.getAddress());
                data.putExtra("shopPhone",info.getMobile());
                setResult(1, data);
                finish();
            }
        });
        setSmartRefreshLayout(mLayoutRefresh);
        showShopList(mUid, Const.DEFAULT_PAGE);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        super.onRefresh(refreshlayout);
        showShopList(mUid, Const.DEFAULT_PAGE);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        super.onLoadmore(refreshlayout);
        showShopList(mUid, String.valueOf(mPage + 1));
    }


    private void showShopList(String uid, String page) {
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("page", page);
        map.put("type", "1");
        mLayoutContent.onLoadingWithContent();
        ObservableHelper.createObservable(RetrofitCreator.createService().shopList(map))
                .subscribe(new SelectorObserver<CountBaseModel<List<ShopInfo>>>() {
                    @Override
                    protected void onErrorCodeInfo(int code, String info) {
                        mLayoutContent.onSuccess();
                        stopRefreshLoad();
                        mLayoutRefresh.setLoadmoreFinished(true);
                        showToast(info);
                    }

                    @Override
                    protected void onSuccess(CountBaseModel<List<ShopInfo>> listBaseModel) {
                        List<ShopInfo> shopInfoList = listBaseModel.getData();
                        mLayoutContent.onSuccess();
                        if (mIsRefresh) {
                            mPage = 0;
                            mShopInfoList.clear();
                            mLayoutRefresh.setLoadmoreFinished(false);
                        } else {
                            mPage++;
                        }
                        stopRefreshLoad();
                        if (!mIsRefresh && shopInfoList.size() < Const.DEFAULT_PAGE_SIZE) {
                            mLayoutRefresh.setLoadmoreFinished(true);
                        }
                        mShopInfoList.addAll(shopInfoList);
                        mChooseShopAdapter.setShopInfoList(mShopInfoList);
                    }

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mLayoutContent.onSuccess();
                        stopRefreshLoad();
                        showToast(e.getMessage());
                    }
                });
    }
}
